package com.xhs.util;

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 代理测试工具类
 *
 * @author xhs
 */
@Slf4j
@Component
public class ProxyTestUtil {

    @Autowired
    private ProxyUtil proxyUtil;

    /**
     * 测试代理连接
     */
    public boolean testProxy() {
        String testUrl = "https://dev.kdlapi.com/testproxy";
        
        log.info("开始测试代理连接: {}", testUrl);
        
        HttpRequest request = HttpRequest.get(testUrl);
        // 强制使用代理进行测试
        request = proxyUtil.setProxy(request);
        
        HttpResponse response = request.execute();
        String result = response.body();
        
        log.info("代理测试结果: {}", result);
        
        return response.getStatus() == 200 && result != null && !result.isEmpty();
    }

    /**
     * 测试代理IP信息
     */
    public String getProxyIpInfo() {
        String testUrl = "https://httpbin.org/ip";
        
        log.info("获取代理IP信息: {}", testUrl);
        
        HttpRequest request = HttpRequest.get(testUrl);
        // 强制使用代理获取IP信息
        request = proxyUtil.setProxy(request);
        
        HttpResponse response = request.execute();
        String result = response.body();
        
        log.info("代理IP信息: {}", result);
        
        return result;
    }
} 